Variables — Exercise Cards (Class room version)¶
Try me¶
How to use¶
Each card below mirrors the A4 activity prompt. First try to complete the exercise by yourself, predict the output or find the bug before running.
Then run the code cell to verify, or run the Fix cell (when provided) to see a correct version.
Keep explanations short and schematic (what/why). ### Rendering Gemini into an interactive AI tutor
If you need help, hints, or extra exercises from AI, use the following prompt to convert it into an AI-tutor
You are a **coding tutor** for Python in Jupyter/Colab. Follow the **course motto** “do not give up learning.”
### Role & Goals
- Use **Socratic guidance** and **test-first thinking** to help me solve problems myself.
- Help me read errors, reason about state, and make small, safe iterations.
### Strict Rules
1) **Do not** provide full working solutions or paste complete functions/programs.
- You may show **tiny illustrative fragments (≤3 lines)** or **pseudo-code with TODOs**, but not a drop-in answer.
2) Prefer **questions over answers**; offer **one small next step** at a time.
3) When debugging, explain **what the traceback says**, give **2–3 hypotheses**, and propose the **smallest diff** in *plain English* first.
4) Encourage **TDD**: ask me to write/assert a test, predict, run, and report outputs.
5) Keep responses concise (≈120–150 words) unless I ask for a deeper explanation or code review.
6) Ask me to **run code and share results**; adapt based on the output.
7) If I request the full solution, remind me of the rules and offer a **higher-tier hint** instead.
8) When I finalize an exercise, reinforce learning lessons and suggest additional exercises
### Interaction Loop (use this structure)
- **Restate goal:** what I’m trying to accomplish in one line.
- **Diagnose:** key assumption to check or error to interpret.
- **Hint (tiered):**
- Tier 1: Conceptual nudge (no code).
- Tier 2: Directed hint (identify line/construct to change).
- Tier 3: Pseudo-code with TODOs or a **1–3 line** pattern (still not a full solution).
- **Next action:** one concrete step for me to try now.
- **Ask back:** what to run/paste (output, test result, or traceback).
### When reviewing my code
- Comment on **correctness, clarity, naming, and complexity (big-O)**.
- Suggest **tests** I’m missing (boundaries, empty cases, error paths).
### Safety & Ethics
- No secrets or private data in prompts.
- avoid library functions/APIs unless I ask.
Stay in tutor mode for the whole session.
Numeric Operations¶
Card N-1 — Human Calculator¶
n = 9//2
n = n**2
# What is the value of n after the script?
[ ]:
# Card N-1 — check
n = 9//2
n = n**2
print(n)
Card N-2 — Human Calculator¶
# y = x % 2
# What is y when x=9? and when x=8?
[ ]:
# Card N-2 — check with two samples (no input)
def parity(x: int) -> int:
return x % 2
print("x=9 -> y=", parity(9))
print("x=8 -> y=", parity(8))
Card N-3 — Code Detective (Find the bug)¶
Buggy Code:
x = ((1 + 2) * 3 / (4 - 0.5)
Fix it so it runs witout errors.
[ ]:
# Card N-3 — fixed
x = ((1 + 2) * 3 / (4 - 0.5)
print(x)
Card N-4 — Human Calculator¶
n = 9//2 - 2 ** 2
# What is the result?
[ ]:
# Card N-4 — check
n = 9//2 - 2 ** 2
print(n)
Card N-5 — Human Calculator¶
x = 4+5j
y = 3+3j
print(x.imag + y.imag)
What is printed?
[ ]:
# Card N-5 — check
x = 4+5j
y = 3+3j
print(x.imag + y.imag)
Strings¶
Card S-1 — Code Detective (Find the bug)¶
Buggy:
my_message = Hello World
Fix the string assignment.
[ ]:
# Card S-1 — fixed
my_message = Hello World
print(my_message) # expected: Hello World
Card S-2 — Human Calculator¶
x = "2"
y = 3
print(x * y)
What is printed?¶
[ ]:
# Card S-2 — check
x = "2"
y = 3
print(x * y)
Original idea:
a = input("What did you say")
print(a + "- duh-duuh")
Question: What does the bot do? (We simulate without input.)
[ ]:
# Card S-3 — simulate without input
a = "I like Python"
print(a + "- duh-duuh") # expected: echoes input with suffix
area = 9 # area covered
total = 10 # total area
print(f"Area = {area}, covered: {area/total:.0%}")
What is printed?
[ ]:
# Card S-4 — check
area = 9
total = 10
print(f"Area = {area}, covered: {area/total:.0%}") # expected: Area = 9, covered: 90%
s = "Hello"
print(s + "!")
print(s.replace("l","L"))
print(s)
What is printed (and why)?
[ ]:
# Card S-5 — check
s = "Hello"
print(s + "!") # Hello!
print(s.replace("l","L")) # HeLLo
print(s) # Hello (strings are immutable)
Boolean Variables¶
Card B-1 — Code Detective (Find the bug)¶
x = 1
y = 2
a = x = y # a boolean variable?
Question: What’s wrong with the comment, and how to get a boolean instead?
[ ]:
# Card B-1 — explanation & fix
x = 1
y = 2
a = x = y
print(a, x, y) # 2 2 2
Card B-2 — Human Calculator¶
Original idea (uses input):
age = int(input("what is your age?"))
print(18 <= age < 65)
print(bool(""))
print(bool("False"))
What gets printed? (Assume user enters the number 18.)
[ ]:
# Card B-2 — simulate without input
age = int(input("What is your age? "))
print(18 <= age < 65) # expected: True
print(bool("")) # expected: False (empty string is falsey)
print(bool("False")) # expected: True (non-empty strings are truthy)
Card B-3 — Human Calculator¶
def ping():
print("ping")
return True
print(False and ping())
print(True or ping())
What is printed?
[ ]:
# Card B-3 — check (short-circuit)
def ping():
print("ping")
return True
print(False and ping()) # expected: False (ping not called)
print(True or ping()) # expected: True (ping not called)
D) Extra Cards¶
Extra N-E1 — Human Calculator¶
print(2 ** 1 + 2)
What is printed, and why?¶
[ ]:
# Extra N-E1 — check (operations priority)
print(2 ** 1 + 2)
Fix the code below to parse the string s ="3.14" into a number.
[ ]:
# Extra N-E2 — demo
s = "3.14"
# print(int(s)) # ValueError if uncommented
val = 3.4 # FIX: correct approach: parse as float
print(val) # expected: 3.14
print(f"bin:{13:b} hex:{255:x}")
What is printed?¶
[ ]:
# Extra S-E4 — check
print(f"bin:{13:b} hex:{255:x}") # expected: bin:1101 hex:ff
a = 1000; b = 1000
print(a == b, a is b)
What is printed, and why?¶
[ ]:
# Extra B-E5 — check
a = 1000; b = 1000
print(a == b, a is b) # expected: True False (typically; equal value, different objects)
Predict the outputs:
print(bool([]), bool([0]))
print(bool({}), bool({"x":0}))
[ ]:
# Extra B-E6 — check
print(bool([]), bool([0])) # expected: False True
print(bool({}), bool({"x":0})) # expected: False True
Avoid using is for string equality.
code = "OK"
# if code is "OK": # anti-pattern
# print("ok")
Fix it.
[ ]:
# Extra B-E7 — fixed
code = "OK"
if code == "OK":
print("ok") # expected: ok
Extra B-8 — Human Calculator Predict the result
print(True and True)
print(not False or False)
print(3 == 4)
print(0.3 == 0.1 + 0.2)
[ ]:
print(True and True)
print(not False or False)
print(3 == 4)
print(0.3 == 0.2 + 0.1)